package ParseHtmlConent2;

import GetTopic.CatchHtmlPage;

import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class GetHtmlContent extends Thread{
	
	public Page page;
	public HtmlContent hc;
	
	public GetHtmlContent(Page page,HtmlContent hc){
		this.page = page;
		this.hc = hc;
	}
	
	public void run(){
		hc.content = this.getContent(page);
	}
	
	/**
	 * @description Get content of a url
	 * @futurework  be better!!
	 * @return      maincontent
	 */
	public String getContent(Page page){
		String content = page.abstractstring;
		
		try{
			CatchHtmlPage chp = new CatchHtmlPage(page.URL);
			
			HtmlPage startPage = chp.catchfile();
			
			System.out.println("HtmlPage  is get......................");
			
			DomNodeList<HtmlElement>  divlist, plist;
			divlist = null;
			plist = null;
			try{
				divlist = startPage.getElementsByTagName("div");
				//System.out.println(divlist.size());
			}catch(Exception e){
				System.out.println("Do not have div.....");
			}
			try{
				plist = startPage.getElementsByTagName("p");
			//System.out.println(plist.size());
			}catch(Exception e){
				System.out.println("Do not have p.....");
			}
			for(HtmlElement e: divlist){
				if(e.asText().trim().length() > 50){
					content = content + e.asText().trim();
				}
			}
			
			for(HtmlElement e: plist){
				if(e.asText().trim().length() > 50){
					content = content + e.asText().trim();
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
				
		return content;
	}
}
